﻿using System;
using System.Numerics;
using System.Collections.Generic;

namespace homework3
{
    public interface  Shape
    {
        bool IsValid();
        double Area();
    }

    public class Square : Shape
    {
        protected double length;

        public Square(double val)
        {
            length = val;
            Console.WriteLine("创建一个正方形，边长是:"+length);
        }

        public bool IsValid()
        {
            if(length <=0)return false;
            else return true;
        }

        public double Area()
        {
            return length * length;
        }
    }

    public class Rectangle : Square
    {
        private double width;

        public Rectangle(double val, double w):base(val) {
            width = w;
            Console.WriteLine("创建一个长方形，边长为:"+length+","+width);
        }


        new public bool IsValid()
        {
            if (length <= 0 || width <= 0) return false;
            else return true;
        }

        new public double Area()
        {
            return length * width;
        }
    }

    public class Circle : Shape
    {
        private double radius;

        public Circle(double val)
        {
            radius = val;
            Console.WriteLine("创建一个圆，边长是" + radius);
        }

        public bool IsValid()
        {
            if((radius <= 0))return false;
            else return true;
        }

        public double Area()
        {
            return Math.PI * radius * radius;
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List<Shape> shapes = new List<Shape>();

            Random random = new Random();
            int remainingCount = 10;

            while (remainingCount > 0)
            {
                int randomNumber = random.Next(1, 4); // 生成1到3之间的随机数，对应Square、Rectangle和Circle三种形状

                switch (randomNumber)
                {
                    case 1:
                        if (remainingCount >= 1)
                        {
                            double side = random.NextDouble() * 10 + 1; // 生成1到11之间的随机数作为Square的边长
                            Console.WriteLine("创建第" + (11 - remainingCount) + "个形状对象");
                            shapes.Add(new Square(side));
                            remainingCount--;
                        }
                        break;
                    case 2:
                        if (remainingCount >= 1)
                        {
                            double length = random.NextDouble() * 10 + 1; // 生成1到11之间的随机数作为Rectangle的长度
                            double width = random.NextDouble() * 10 + 1; // 生成1到11之间的随机数作为Rectangle的宽度
                            Console.WriteLine("创建第" + (11 - remainingCount) + "个形状对象");
                            shapes.Add(new Rectangle(length, width));
                            remainingCount --;
                        }
                        break;
                    case 3:
                        if (remainingCount >= 1)
                        {
                            double radius = random.NextDouble() * 10 + 1; // 生成1到11之间的随机数作为Circle的半径
                            Console.WriteLine("创建第" + (11 - remainingCount) + "个形状对象");
                            shapes.Add(new Circle(radius));
                            remainingCount--;
                        }
                        break;
                }
            }

            // 计算所有形状的面积之和
            double totalArea = 0;
            foreach (var shape in shapes)
            {
                totalArea += shape.Area();
            }

            Console.WriteLine("总面积:" + totalArea);
        }

    }
}
